// Author : Ajeng Ritanita Nur Widiati - 091511036

package jtk.project4.fleet.screen.addMaintenanceTask;

import java.awt.FlowLayout;
import java.util.List;

import jtk.project4.fleet.action.CancelAction;
import jtk.project4.fleet.action.CancelMaintenanceTaskAction;
import jtk.project4.fleet.action.SaveMaintenanceTaskAction;
import jtk.project4.fleet.action.SubmitAction;
import jtk.project4.fleet.event.AddMaintenanceTaskEvent;
import jtk.project4.fleet.field.addMaintenanceTaskField;

import nl.coderight.jazz.action.Action;
import nl.coderight.jazz.dialog.MessageType;
import nl.coderight.jazz.dialog.OptionDialog;
import nl.coderight.jazz.dialog.OptionType;
import nl.coderight.jazz.dialog.ReturnOptionType;
import nl.coderight.jazz.form.FormHeader;
import nl.coderight.jazz.form.FormView;
import nl.coderight.jazz.form.control.GroupControl;
import nl.coderight.jazz.form.field.button.PushButton;
import nl.coderight.jazz.form.validation.ValidationError;

public class AddMaintenanceTaskView extends FormView{
	private FormHeader formHeader;
	private PushButton submitButton;
	private PushButton submitButton2;
	private PushButton cancelButton;
	private Action submitAction;
	private Action submitAction2;
	private Action cancelAction;
	
	public void handleEvent(AddMaintenanceTaskEvent evt) {
		switch (evt.getType()) {
		case CANCEL:
			close();
			break;
			
		case SAVE:
		
			// check for validation errors
			List<ValidationError> errors = validate();
			if (!errors.isEmpty()) {
				// there are validation errors so do not submit...
				errors.get(0).getField().focus();
				break;
			} else {
				// submit form and close view
				submit();
				close();
				// fall-through!
			}
			
		default:
			// propagate event to controller
			propagateEvent(evt);
			break;
		}
	}
	
	@Override
	public void close() {
		// check if form is changed, if true show dialog
		if (isChanged()) {
			OptionDialog dialog = new OptionDialog(this);
			dialog.setMessageType(MessageType.QUESTION);
			dialog.setTitle("dialog.discard.title");
			dialog.setMessage("dialog.discard.message");
			dialog.setOptionType(OptionType.YES_NO);
			dialog.setOptions("button.yes", "button.no","");
			dialog.show();
			if (dialog.getReturnOptionType() == ReturnOptionType.NO) {
				focus();
				return;
			}
		}
		super.close();
	}
	
	
	@Override
	protected void init() {
		createAction();
		createFields();
		createLayout();		
	}	
	
	private void createAction() {
		submitAction = new SubmitAction();
		submitAction2 = new SubmitAction(); // cara buat save+ di message box?
		cancelAction = new CancelAction();
		// enable submit button when form is changed
		setOnChangeAction(submitAction);
	}
	
	private void createFields(){
		submitButton = new PushButton(new SaveMaintenanceTaskAction());
		submitButton2 = new PushButton(submitAction2);
		cancelButton = new PushButton(new CancelMaintenanceTaskAction());
	}
	
	private void createLayout() {
		setHeader(createHeader());
		addField(createForm());
	}
	
	private FormHeader createHeader() {
		formHeader = new FormHeader();
		formHeader.setTitle("addMaintenanceTask.header.form");
		return formHeader;
	}
	
	private GroupControl createForm() {
		GroupControl form = new GroupControl();
		form.addField(new addMaintenanceTaskField("PmHist"), NORTH);
		form.addField(createButtons(), SOUTH);		
		return form;
	}
	
	private GroupControl createButtons() {
		GroupControl buttons = new GroupControl();
		buttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
		buttons.addField(submitButton);
		buttons.addField(submitButton2);
		buttons.addField(cancelButton);
		return buttons;
	}
	
	

}
